package yxy.game.pm2.module.coin;

import java.util.Map;

import org.joda.time.Minutes;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.levy;
import yxy.cherry.data.bean.levydate;
import yxy.cherry.data.bean.map;
import yxy.cherry.data.bean.vipdata;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.conquest.ConquestHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

public class CoinBean {
    // bean===========================================================================

    private int dataId;
    private int times;

    public int getDataId() {
        return dataId;
    }

    public void setDataId(int dataId) {
        this.dataId = dataId;
    }

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    // func===========================================================================

    public int times() {
        levydate data = data();
        return data.getMaximum() - getTimes();
    }

    public Currency cost() {
        levydate data = data();
        return Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
    }

    public Currency reward(String playerCode) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        levydate data = data();
        Map<String, levy> rewardDates = DataCenter.getAllData(levy.class);
        levy bestdata = null;
        for (levy _levy : rewardDates.values()) {
            if (_levy.getType() == data.getType()) {
                bestdata = _levy;
            }
        }

        //计算银币数量
        long silverNum = getSilver(playerCode, bestdata.getTime());
        Currency reward = Currency.create(bestdata.getRewardtype(), bestdata.getRewardid(), silverNum);
        return vipReward(playerBean, reward);
    }


    /**
     * 银币数量
     *
     * @param playerCode
     * @param time
     * @return
     */
    public static long getSilver(String playerCode, int time) {
        try {
            long passTime = time * 60 * 1000;
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            map _mapdata = DataCenter.getData(conquestModule.getMapId(), map.class);
            long duration = Minutes.minutes(1).toStandardDuration().getMillis();
            long coin = _mapdata.getCoin() * passTime / duration;
            return coin;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * vip特权加成
     */
    private Currency vipReward(PlayerBean playerBean, Currency reward) {
        int viplevel = playerBean.getViplevel();
        vipdata _vipdata = DataCenter.getData(viplevel, vipdata.class);
        if (_vipdata == null) {
            return reward;
        }
        if (reward.getType().equals(CurrencyType.CT_钱币)) {
            reward.setNumber(reward.getNumber() + reward.getNumber() * _vipdata.getVar5() / 10000);
        }
        return reward;
    }

    public static long vipRewardCoinNum(String playerCode, long num) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        int viplevel = playerBean.getViplevel();
        vipdata _vipdata = DataCenter.getData(viplevel, vipdata.class);
        if (_vipdata == null) {
            return num;
        }

        return num + num * _vipdata.getVar5() / 10000;
    }


    private levydate data() {
        return DataCenter.getData(getDataId(), levydate.class);
    }

}
